import { between, body, OperationContext, param, query, required, RouterRequest, validate } from "@atinc/chaos/router";
import { Id } from "@atinc/chaos/repository";
import { _ } from "@atinc/chaos";
import { Is } from "@atinc/chaos/constants";

export class GetUserRequest extends RouterRequest<OperationContext> {

    @param("id", (source, key) => _.parseId(source[key]))
    @required("id参数不能为空")
    id: Id;

    @query("deleted")
    deleted?: Is;

    @query("archived")
    archived?: Is;
}

export class UpdateUserRequest extends RouterRequest<OperationContext> {

    @param("id", (source, key) => _.parseId(source[key]))
    id: Id;

    @body()
    @required("姓名不能为空")
    username: string;

    @body()
    @between(6, 18, "密码长度为6到18位")
    password: string;

    @body()
    @validate(async (value: string, propertyKey, request, ctx) => {
        console.log(value);
        console.log(propertyKey);
        console.log(request);
        console.log(ctx);
        return ["male", "female"].includes(value);
    })
    sex: string;


}

//克隆一个请求类
// TODO 是否由简单写法
export class CreateUserRequest extends RouterRequest<OperationContext> {
    @body()
    @required("姓名不能为空")
    public username: string;

    @body()
    @required("密码长度为6到18位")
    public password: string;

    @body()
    @validate(async (value: string, propertyKey, request, ctx) => {
        console.log(value);
        console.log(propertyKey);
        console.log(request);
        return ["male", "female"].includes(value[propertyKey]);
    })
    public sex: string;
}
